﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

#include <QFrame>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QImage>
#include <QSize>
#include <QFileInfo>

// custom headers
#include "LegendEditingCard.h"
//#include "NewUrlImageProvider.h"
#include "ILegend.h"
#include "AppService.h"
#include "TranslatorManager.h"
#include "GbmpNew.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 
namespace gmepgcs
{
    LegendEditingCard::LegendEditingCard(QString title, QWidget *parent) :
        QWidget(parent),
        m_spLegend(nullptr),
        m_title(title),
        m_legendDisplaySize(128, 128 - 32),
        pLblLegendImage(nullptr),
        pBtnDelete(nullptr)
    {
        Init();
    }

    LegendEditingCard::~LegendEditingCard()
    {
    }

    void LegendEditingCard::SetLegend(std::shared_ptr<ILegend> spLegend)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(pBtnDelete, L"pBtnDelete为空", L"GMEP", L"2024-01-30");
        DBG_WARN_AND_RETURN_VOID_UNLESS(pLblLegendImage, L"pLblLegendImage为空", L"GMEP", L"2024-01-30");
        std::shared_ptr<ILegend> oldLegend = m_spLegend;
        if (oldLegend != spLegend)
        {
            m_spLegend = spLegend;
            emit LegendChanged(oldLegend, m_spLegend);
        }
        
        if (!m_spLegend)
        {
            pBtnDelete->setEnabled(false);

            pLblLegendImage->clear();
            pLblLegendImage->setText(QString::fromStdWString(GBMP_TR(L"未设置")));
        }
        else
        {
            pBtnDelete->setEnabled(true);            
            DBG_WARN_AND_RETURN_VOID_UNLESS(pLblLegendImage, L"pLblLegendImage为空", L"GMEP", L"2024-01-30");
            pLblLegendImage->clear();
            pLblLegendImage->setToolTip(QString::fromStdWString(m_spLegend->Name));
            if(m_spLegend->Thumbnail.empty())
            {
                pLblLegendImage->setText(QString::fromStdWString(GBMP_TR(L"无缩略图")));
            }
            else
            {
                DBG_WARN_AND_RETURN_VOID_UNLESS(m_spLegend, L"m_spLegend为空", L"GMEP", L"2024-01-30");
                QString path = QString::fromStdWString(m_spLegend->Thumbnail);
                if (path.isEmpty() || !QFileInfo(path).exists())
                {
                    DBG_WARN_AND_RETURN_VOID_UNLESS(pLblLegendImage, L"pLblLegendImage为空", L"GMEP", L"2024-01-30");
                    pLblLegendImage->setText(QString::fromStdWString(GBMP_TR(L"缩略图加载失败")));
                    return;
                }

                QImage imgae;
                imgae.load(path);
                Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio;
                QSize originalSize = imgae.size();
                QSize targetSize = originalSize.scaled(m_legendDisplaySize, aspectRatioMode);

                QImage finalImage = imgae;
                if (originalSize != targetSize)
                {
                    finalImage = imgae.scaled(targetSize, aspectRatioMode, Qt::SmoothTransformation);
                }
                DBG_WARN_AND_RETURN_VOID_UNLESS(pLblLegendImage, L"pLblLegendImage为空", L"GMEP", L"2024-01-30");
                pLblLegendImage->setPixmap(QPixmap::fromImage(finalImage));
            }
        }
    }

    void LegendEditingCard::Init()
    {
        QLabel *pLblTitle = NEW_AS_QT(QLabel,m_title,this);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pLblTitle, L"pLblTitle为空", L"GMEP", L"2024-01-30");
        pLblTitle->setStyleSheet("QLabel{"
            "color:rgba(0, 0, 0, 255);"
            "font-family:\"Microsoft YaHei\";"
            "font-size:12px;"
            "}"
        );

        QString btnStyle("QPushButton{"
            "border:none;"
            "background-color:transparent;"
            "}"
        );

        QCursor btnCursor = QCursor(Qt::CursorShape::PointingHandCursor);

        pBtnDelete = NEW_AS_QT(QPushButton,QIcon(":/images/icon_delete_bin_solid_373F48_16_16.png"), QString(), this);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pBtnDelete, L"pBtnDelete为空", L"GMEP", L"2024-01-30");
        pBtnDelete->setObjectName("DeleteButton");
        pBtnDelete->setFixedSize(QSize(12,12));
        pBtnDelete->setCursor(btnCursor);
        pBtnDelete->setStyleSheet(btnStyle);
        pBtnDelete->setEnabled(false);
        connect(pBtnDelete, &QPushButton::clicked, [=]() {
            emit RemovingLegendRequired(m_spLegend);
        });

        QPushButton *pBtnAdd = NEW_AS_QT(QPushButton,QIcon(":/images/icon_plus_sharp_373F48_16_16.png"), QString(), this);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pBtnAdd, L"pBtnAdd为空", L"GMEP", L"2024-01-30");
        pBtnAdd->setObjectName("AddButton");
        pBtnAdd->setFixedSize(QSize(12,12));
        pBtnAdd->setCursor(btnCursor);
        pBtnAdd->setStyleSheet(btnStyle);
        connect(pBtnAdd, &QPushButton::clicked, [=]() {
            emit ChangingLegendRequired(m_spLegend);
        });

        // 非机电应用，禁止修改图例
        if (!AppService::Get()->IsGmep())
        {
            pBtnDelete->setVisible(false);
            pBtnAdd->setVisible(false);
        }

        QHBoxLayout *pHlHeader = NEW_AS_QT(QHBoxLayout);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pHlHeader, L"pHlHeader为空", L"GMEP", L"2024-01-30");
        pHlHeader->setContentsMargins(8, 8, 8, 8);
        pHlHeader->setSpacing(8);
        pHlHeader->addWidget(pLblTitle);
        pHlHeader->addStretch();
        pHlHeader->addWidget(pBtnDelete);
        pHlHeader->addWidget(pBtnAdd);
        
        QFrame *pFmHeader = NEW_AS_QT(QFrame);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pFmHeader, L"pFmHeader为空", L"GMEP", L"2024-01-30");
        pFmHeader->setFixedHeight(32);
        QString frameName = QString::fromStdWString(L"HeaderFrame-%1").arg(m_title);
        pFmHeader->setObjectName(frameName);
        pFmHeader->setStyleSheet("QFrame#HeaderFrame{"
            "background-color:rgba(217, 221, 228);"
            "}"
        );
        pFmHeader->setLayout(pHlHeader);

        pLblLegendImage = NEW_AS_QT(QLabel);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pLblLegendImage, L"pLblLegendImage为空", L"GMEP", L"2024-01-30");
        pLblLegendImage->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        pLblLegendImage->setAlignment(Qt::AlignCenter);
        pLblLegendImage->setObjectName("LegendImageLabel");
        pLblLegendImage->setStyleSheet(
            "QLabel#LegendImageLabel{"
            "font-family:\"Microsoft YaHei\";"
            "font-size:12px;"
            "color:rgba(160, 162, 171, 255);"
            "}"
        );

        QVBoxLayout *pVlContents = NEW_AS_QT(QVBoxLayout);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pVlContents, L"pVlContents为空", L"GMEP", L"2024-01-30");
        pVlContents->setContentsMargins(0, 0, 0, 0);
        pVlContents->setSpacing(0);       
        pVlContents->addWidget(pFmHeader);
        pVlContents->setAlignment(pFmHeader, Qt::AlignTop);
        pVlContents->addWidget(pLblLegendImage);

        QFrame *pFmWhole = NEW_AS_QT(QFrame);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pFmWhole, L"pFmWhole为空", L"GMEP", L"2024-01-30");
        pFmWhole->setObjectName("WholeFrame");
        pFmWhole->setStyleSheet("QFrame#WholeFrame{"
            "border:1px solid rgba(189, 199, 213, 255);"
            "}"
        );
        pFmWhole->setLayout(pVlContents);

        QHBoxLayout *pHlWhole = NEW_AS_QT(QHBoxLayout);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pHlWhole, L"pHlWhole为空", L"GMEP", L"2024-01-30");
        pHlWhole->setContentsMargins(0, 0, 0, 0);
        pHlWhole->setSpacing(0);
        pHlWhole->addWidget(pFmWhole);

        setLayout(pHlWhole);
        setMinimumSize(QSize(256, 256));
        setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    }
}
